package com.txc.common.global.result;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 通用响应数据结构
 *
 * @ClassName: Result
 * @Author: TXC
 * @Date: 2022-07-29 15:45
 **/
@Data
@NoArgsConstructor
public class Result<T> implements Serializable {
    /**
     * 状态码
     */
    private Integer code;

    /**
     * 返回消息
     */
    private String message;

    /**
     * 状态
     */
    private boolean success;

    /**
     * 返回数据
     */
    private T data;

    public Result(Integer code, String message, boolean success, T data) {
        this.code = code;
        this.message = message;
        this.success = success;
        this.data = data;
    }

    public Result(IResultCode resultCode, boolean success, T data) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        this.success = success;
        this.data = data;
    }

    public Result(IResultCode resultCode, boolean success) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        this.success = success;
        this.data = null;
    }

    public static <T> Result<T> ok() {
        return new Result<>(ResultCode.OK, true);
    }

    public static <T> Result<T> message(String message) {
        return new Result<>(ResultCode.OK.getCode(), message, true, null);
    }

    public static <T> Result<T> ok(T data) {
        return new Result<>(ResultCode.OK, true, data);
    }

    public static <T> Result<T> fail() {
        return new Result<>(ResultCode.ERROR, false);
    }

    public static <T> Result<T> fail(IResultCode resultCode) {
        return new Result<>(resultCode, false);
    }

    public static <T> Result<T> fail(Integer code, String message) {
        return new Result<>(code, message, false, null);
    }

    public static <T> Result<T> fail(IResultCode resultCode, T data) {
        return new Result<>(resultCode, false, data);
    }

    public static <T> Result<T> fail(Integer code, String message, T data) {
        return new Result<>(code, message, false, data);
    }

}
